package util
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	
	public class FPSUtil extends Sprite
	{
		private var _textField:TextField;
		private var _timer:Timer;
		private var _frames:int;
		
		public function FPSUtil()
		{
			super();
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			init();
		}
		
		public function init():void {
			this.graphics.beginFill(0,.5);
			this.graphics.drawRect(0,0,100,50);
			this.graphics.endFill();
			_textField = new TextField();
			_textField.autoSize = TextFieldAutoSize.LEFT;
			_textField.textColor = 0xffffff;
			this.addChild(_textField);
			_timer = new Timer(1000);
			_timer.addEventListener(TimerEvent.TIMER,onTimer);
			_timer.start();
		}
		
		public function onTimer(event:TimerEvent):void {
			_textField.text = _frames+"  FPS";
			_frames = 0;
		}
		
		public function onEnterFrame(event:Event):void {
			_frames ++;
		}
	}
}